var express = require('express');
var router = express.Router();
var mysql = require('mysql');
const app = express()
const multiparty = require("multiparty");
var connection = require('./connection');

function randomString(e) {
  e = e || 32;
  var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
    a = t.length,
    n = "";
  for (i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
  return n
}
// 收藏商品
router.post('/set_love', function (req, res) {
  var love = req.body.lv;
  var id=req.body.id;
  var sql= `update goods_details set isLove="${love}" where id="${id}"`;
  console.log(sql)
  connection.query(sql, function (err, result) {
    if (err) {
		console.log(err)
      res.json({
        code: 500,
        message: '操作失败'
      })
    } else {
      res.json({
        code: 200,
        message: '操作成功'
      })
    }
  })
})

//details
router.post('/goods_details', function (req, res) {
  var id = req.body.id;
  var sql = `SELECT * FROM goods_details where id="${id}"`;
  connection.query(sql, function (err, result) {
    if (err) {
      res.json({
        code: 500,
        message: err
      })
    } else {
      res.json({
        code: 200,
        message: result
      })
    }
  });
})
//上传多张商品详情
router.post('/banner_details_more', function (req, res, next) {
  let form = new multiparty.Form();
  form.uploadDir = "./public/images";
  // form.keepExtensions=true;   //是否保留后缀
  form.parse(req, function (err, fields, files) { //其中fields表示你提交的表单数据对象，files表示你提交的文件对象
    if (err) {
      res.json({
        status: "1",
        msg: "上传失败！" + err
      });
    } else {
      res.json({
        status: "200",
        msg: "上传成功！",
        imgSrc: 'http://192.168.56.1:3000/' + files.file[0].path.replace(/\\/g, "/")
      });
    }
  })
});
//增加详情信息
router.post('/add_details',function(req,res,next){
  var name_ = req.body.name_details;
  var price_ = req.body.price_details;
  var time_ = req.body.time_details;
  var banner_ = req.body.banner_img;
  var details_banner=req.body.details_banner_more.toString();
  var id =req.body.id;
  console.log(req.body)
  var sql = `UPDATE goods_details SET goods_name="${name_}",price="${price_}",img_banner="${banner_}",time="${time_}",img_details="${details_banner}" WHERE id="${id}"`
  connection.query(sql, function (err, result) {
    if (err) {
      res.json({
        code: 500,
        message: err
      })
    } else {
      res.json({
        code: 200,
        message: '增加成功'
      })
    }
  });
})
//上传商品详情banner
router.post('/banner_details', function (req, res, next) {
  let form = new multiparty.Form();
  form.uploadDir = "./public/images";
  // form.keepExtensions=true;   //是否保留后缀
  form.parse(req, function (err, fields, files) { //其中fields表示你提交的表单数据对象，files表示你提交的文件对象
    if (err) {
      res.json({
        status: "1",
        msg: "上传失败！" + err
      });
    } else {
      res.json({
        status: "200",
        msg: "上传成功！",
        imgSrc: 'http://192.168.56.1:3000/' + files.file[0].path.replace(/\\/g, "/")
      });
    }
  })
});
//上传商品标题图片
router.post('/upload', function (req, res, next) {
  let form = new multiparty.Form();
  form.uploadDir = "./public/images";
  // form.keepExtensions=true;   //是否保留后缀
  form.parse(req, function (err, fields, files) { //其中fields表示你提交的表单数据对象，files表示你提交的文件对象
    if (err) {
      res.json({
        status: "1",
        msg: "上传失败！" + err
      });
    } else {
      res.json({
        status: "200",
        msg: "上传成功！",
        imgSrc: 'http://192.168.56.1:3000/' + files.file[0].path.replace(/\\/g, "/")
      });
    }
  })
});
//删除后台商品
router.post('/delete', function (req, res, next) {
  var id = req.body.id;
  connection.query(`delete from goods where id="${id}";delete from goods_details where id="${id}"`, function (err, result) {
    if (err) {
      res.json({
        code: 500,
        message: err
      })
    } else {
      res.json({
        code: 200,
        message: '删除成功'
      })
    }
  });
})
//后台修改商品
router.post('/update_product', function (req, res) {
  var name = req.body.name;
  var price = req.body.price;
  var img = req.body.img;
  var old_p = req.body.oldprice;
  var id = req.body.id;
  var sql = `UPDATE goods SET goodName="${name}",goodPrice="${price}",img="${img}",oldPrice="${old_p}" WHERE id="${id}"`
  connection.query(sql, function (err, result) {
    if (err) {
      res.json({
        code: 500,
        message: err
      })
    } else {
      res.json({
        code: 200,
        message: '修改成功'
      })
    }
  });
})
//后台增加商品
router.post('/add_product', function (req, res) {
  var name = req.body.name;
  var price = req.body.price;
  var img = req.body.img;
  var old_p = req.body.oldprice;
  var id = randomString(9)
  var sql = `INSERT INTO goods VALUES ("${name}","${price}","${img}","${old_p}","${id}");INSERT INTO goods_details VALUES ("","","","","","${id}","0")`
  connection.query(sql, function (err, result) {
    if (err) {
      res.json({
        code: 500,
        message: err
      })
    } else {
      res.json({
        code: 200,
        message: '增加成功'
      })
    }
  });
})
// 秒杀接口
router.get('/miaosha', function (req, res) {
  console.log(req.body)
  var sql = 'SELECT * FROM miaosha';
  connection.query(sql, function (err, result) {
    if (err) {

      res.json({
        code: 500,
        message: err
      })
    } else {
      res.json({
        code: 200,
        message: result
      })
    }
  });
});
router.get('/look', function (req, res) {
  console.log(req.body)
  var sql = 'SELECT * FROM goods';
  connection.query(sql, function (err, result) {
    if (err) {
      res.json({
        code: 500,
        message: err
      })
    } else {
      res.json({
        code: 200,
        message: result
      })
    }
  });
});
/* GET home page. */
router.post('/', function (req, res, next) {
  res.render('index', {
    title: 'Express'
  });
});

module.exports = router;